home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0039_Complex Numbers.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  1KB  |  50 lines

  1. {
  2. >A>overlooked. No Pascal compiler that I know of (including Turbo) can return
  3. >A>a complex value (i.e., a record or an array) from a FUNCTION. In order for
  4. >
  5. >Hmm...never tried this before. Anyway, the sollution is quite simple:
  6. >just have the megaword-variable public, and pass it to the procedure.
  7.  
  8. Returning function values by setting a public variable is pretty dangerous -
  9. what if your function calls another that uses the same public to return its
  10. value?  In this case, it's not necessary, since there's a trick to let TP
  11. return complex numbers:
  12. }
  13.  
  14. type
  15.   Float = Double;
  16.   TComplex = string[2*sizeof(float)];
  17.   { Complex number.  Not a true string:  the values are stored in binary
  18.     format within it. }
  19.  
  20.   TCmplx = record   { The internal storage format for TComplex }
  21.     len : byte;
  22.     r,i : float;
  23.   end;
  24.  
  25. function Re(z:TComplex):float;
  26. begin
  27.   Re := TCmplx(z).r;
  28. end;
  29.  
  30. function Im(z:TComplex):float;
  31. begin
  32.   Im := TCmplx(z).i;
  33. end;
  34.  
  35. function Complex(x,y:float):TComplex;
  36. { Convert x + iy to complex number. }
  37. var
  38.   result : TCmplx;
  39. begin
  40.   with result do
  41.   begin
  42.     len := 2*sizeof(float);
  43.     r := x;
  44.     i := y;
  45.   end;
  46.   Complex := TComplex(result);
  47. end;
  48.  
  49. {You can use these to build up lots of functions returning TComplex types.}
  50.